//
//  NSCustomTransparentView.m
//  MacDevTest
//
//  Created by lbxia on 2022/9/3.
//  Copyright © 2022 com.lbx. All rights reserved.
//

#import "NSCustomTransparentView.h"

@interface NSCustomTransparentView()
@property (nonatomic, weak) NSView *allowView;
@end


@implementation NSCustomTransparentView

- (NSView*)allowView {
    if (!_allowView) {
        
        if (self.window.hasTitleBar) {
    //      NSTitlebarContainerView
                        
            NSView *superView = self.superview;
            
            for (NSView *view in superView.subviews) {
                
                if ([view isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) {
                    
                    _allowView = view;
                }
            }
        }
    }
    return _allowView;
}


- (void)viewDidMoveToSuperview{
    
    [super viewDidMoveToSuperview];
    
//    self.translatesAutoresizingMaskIntoConstraints = NO;
//
//    NSView *superView = self.superview;
//    
//    [self.centerYAnchor constraintEqualToAnchor:superView.centerYAnchor].active = YES;
//    [self.centerXAnchor constraintEqualToAnchor:superView.centerXAnchor].active = YES;
//    [self.widthAnchor constraintEqualToAnchor:superView.widthAnchor].active = YES;
//    [self.heightAnchor constraintEqualToAnchor:superView.heightAnchor].active = YES;
}

- (void)mouseDown:(NSEvent *)event {

    NSPoint point = [event locationInWindow];
   
    if (self.window.hasTitleBar && self.allowView) {
        
        NSView *view = self.allowView;
        
        if (point.y > view.frame.origin.y && point.y < (view.frame.origin.y + view.frame.size.height) ) {
            
            [super mouseDown:event];
        }
    }
}

- (void)mouseUp:(NSEvent *)event {

    NSPoint point = [event locationInWindow];
    
    NSView *superView = self.superview;
    
    for (NSView *view in superView.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) {
            
            if (point.y > view.frame.origin.y && point.y < (view.frame.origin.y + view.frame.size.height) ) {
                
                [super mouseUp:event];
                break;
            }
        }
    }
}

@end
